package com.lzm.ydpt.module.mall.activity;

import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.basemvp.BasePresenter;
import com.byt.framlib.commonutils.KeyBordUtil;
import com.byt.framlib.commonwidget.ClearableEditText;
import com.byt.framlib.commonwidget.FlowLayout;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.byt.framlib.commonwidget.onclicktwo.AntiShake;
import com.lzm.ydpt.BaseColor;
import com.lzm.ydpt.Constants;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.SearchWords;
import com.lzm.ydpt.module.mall.fragment.ProductListFragment;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商品搜索
 */
public class ProductSearchActivity extends BaseActivity {

    @BindView(R.id.cet_search_content)
    ClearableEditText cet_search_content;

    @BindView(R.id.ll_search_layout)
    LinearLayout ll_search_layout;
    @BindView(R.id.fl_search_record)
    FlowLayout fl_search_record;

    @BindView(R.id.fl_search_content)
    FrameLayout fl_search_content;

    private List<SearchWords> strList = new ArrayList<>();
    private String keyword = "";
    private long shop_id = 0L;

    private FragmentTransaction fragmentTransaction = null;
    private FragmentManager fragmentManager = null;
    private ProductListFragment mProductListFragmen = null;

    @Override
    public int getLayoutId() {
        return R.layout.activity_mall_product_search;
    }

    @Override
    public BasePresenter initPreData() {
        return null;
    }

    @Override
    public void initView() {
        shop_id = getIntent().getLongExtra(Constants.INP_SHOP_ID, 0L);
        initData();
        initTab();
        cet_search_content.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //关闭软键盘
                    KeyBordUtil.hideSoftKeyboard(cet_search_content);
                    keyword = cet_search_content.getText().toString();
                    if (TextUtils.isEmpty(keyword)) {
                        showShortToast("请输入需要搜索的内容");
                        return true;
                    }
                    ll_search_layout.setVisibility(View.GONE);
                    fl_search_content.setVisibility(View.VISIBLE);
                    //搜索
                    showContent();
                    boolean isSave = true;
                    if (strList.size() > 0) {
                        for (SearchWords word : strList) {
                            if (word.getKeywords().equals(keyword)) {
                                isSave = false;
                                break;
                            }
                        }
                    }
                    if (isSave) {
                        SearchWords searchWords = new SearchWords();
                        searchWords.setType(SearchWords.TYPE_SHOP);
                        searchWords.setKeywords(keyword);
                        searchWords.setTime(System.currentTimeMillis());
                        searchWords.save();
                    }
                    return true;
                }
                return false;
            }
        });
    }

    private void showContent() {
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.show(mProductListFragmen);
        fragmentTransaction.commit();
        mProductListFragmen.setKeyword(keyword);
    }

    public void initData() {
        if (!strList.isEmpty()) {
            strList.clear();
        }
        strList.addAll(LitePal.where("type = ?", SearchWords.TYPE_SHOP + "").order("time desc").limit(10).find(SearchWords.class));
        ll_search_layout.setVisibility(strList.size() == 0 ? View.GONE : View.VISIBLE);
        initSearchRecipesLabel();
    }

    private void initTab() {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        mProductListFragmen = ProductListFragment.newInstance(shop_id);
        fragmentTransaction.add(R.id.fl_search_content, mProductListFragmen);
        fragmentTransaction.hide(mProductListFragmen);
        fragmentTransaction.commit();
    }

    public void initSearchRecipesLabel() {
        fl_search_record.removeAllViews();
        for (SearchWords searchWords : strList) {
            TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.comment_search_tag_tv, fl_search_record, false);
            tv.setText(searchWords.getKeywords());
            GradientDrawable drawable = new GradientDrawable();
            drawable.setColor(BaseColor.color_999999);
            drawable.setCornerRadius(getResources().getDimension(R.dimen.x100));
            tv.setBackground(drawable);
            tv.setTextSize(14f);
            tv.setTextColor(BaseColor.color_666666);
            tv.setOnClickListener(new OnNoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    keyword = searchWords.getKeywords();
                    cet_search_content.setText(keyword);
                    ll_search_layout.setVisibility(View.GONE);
                    fl_search_content.setVisibility(View.VISIBLE);
                    //搜索
                    showContent();
                }
            });
            fl_search_record.addView(tv);
        }
    }

    @OnClick({R.id.tv_product_search_back, R.id.img_clear_search_content})
    public void OnClick(View v) {
        if (AntiShake.check(v.getId())) {
            return;
        }
        switch (v.getId()) {
            case R.id.tv_product_search_back:
                finish();
                break;
            case R.id.img_clear_search_content:
                strList.clear();
                ll_search_layout.setVisibility(strList.size() == 0 ? View.GONE : View.VISIBLE);
                initSearchRecipesLabel();
                LitePal.deleteAll(SearchWords.class, "type = ?", SearchWords.TYPE_SHOP + "");
                break;
        }
    }
}